package com.congwulin.wechat.util.service.bill;

import com.congwulin.wechat.entity.common.AbstractMeBillVO;
import com.congwulin.wechat.entity.common.SuperVO;
import com.congwulin.wechat.util.interfaces.AbstractBillService;
import com.congwulin.wechat.util.EntityConst;
import com.congwulin.wechat.util.interfaces.IBillPersistent;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class BillDeleteService extends AbstractBillService{

    /**
     * 一主多子实体抽象服务构造函数
     *
     * @param dao 一主多子实体保存数据库器
     */
    public BillDeleteService(IBillPersistent dao) {
        super(dao);
    }

    /**
     * 删除一主多子实体数组
     *
     * @param vos 一主多子实体数组
     */
    public void delete(AbstractMeBillVO[] vos) {
        List<AbstractMeBillVO> list = Arrays.asList(vos);
        this.delete(list);
    }

    /**
     * 删除一主多子实体列表
     *
     * @param list 一主多子实体列表
     */
    @SuppressWarnings("unchecked")
    public void delete(List<?> list) {
        List<AbstractMeBillVO> paralist = (List<AbstractMeBillVO>) list;

        BillTool tool = new BillTool();
        tool.setStatus(paralist, EntityConst.DELETE);
        Map<Class<?>, List<SuperVO>> values = tool.group(paralist, EntityConst.DELETE);
        Map<String,String> lockmap = null;
        try {
            // 加锁、校验时间戳 TODO
            lockmap = this.lock(values);
            this.checkTs(values);

            this.getDAO().delete(paralist);
        }
        finally {
            //释放锁 TODO
            this.unlock(lockmap);
        }
    }
}
